package com.automailerbot.controller;

import com.automailerbot.registration.OutlookRegisterService;
import com.automailerbot.alias.OutlookAliasManager;
import com.automailerbot.mailreader.OutlookMailReader;
import com.automailerbot.mailreader.GmailMailReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 邮件机器人接口控制器
 */
@RestController
@RequestMapping("/api/mail")
public class MailBotController {

    /**
     * 健康检查端点
     */
    @GetMapping("/health")
    public ResponseEntity<String> healthCheck() {
        return ResponseEntity.ok("✅ 服务运行正常");
    }

    @Autowired
    private OutlookRegisterService outlookRegisterService;

    @Autowired
    private OutlookAliasManager outlookAliasManager;

    @Autowired
    private OutlookMailReader outlookMailReader;

    @Autowired
    private GmailMailReader gmailMailReader;

    /**
     * 注册 Outlook 账户
     */
    @PostMapping("/register/outlook")
    public ResponseEntity<String> registerOutlook(@RequestParam String username,
                                                  @RequestParam String password) {
        try {
            outlookRegisterService.registerAccount(username, password);
            return ResponseEntity.ok("✅ 注册已发起：" + username + "@outlook.com");
        } catch (Exception e) {
            return ResponseEntity.status(500).body("❌ 注册失败: " + e.getMessage());
        }
    }

    /**
     * 添加 Outlook 别名
     */
    @PostMapping("/alias/outlook")
    public ResponseEntity<String> addOutlookAlias(@RequestParam String email,
                                                  @RequestParam String password,
                                                  @RequestParam String alias) {
        try {
            outlookAliasManager.addAlias(email, password, alias);
            return ResponseEntity.ok("✅ 别名添加成功：" + alias + "@outlook.com");
        } catch (Exception e) {
            return ResponseEntity.status(500).body("❌ 添加别名失败: " + e.getMessage());
        }
    }

    /**
     * 读取 Outlook 邮箱
     */
    @GetMapping("/read/outlook")
    public ResponseEntity<String> readOutlookInbox(@RequestParam String email,
                                                   @RequestParam String password) {
        try {
            outlookMailReader.readInbox(email, password);
            return ResponseEntity.ok("✅ Outlook 邮件读取成功");
        } catch (Exception e) {
            return ResponseEntity.status(500).body("❌ 邮件读取失败: " + e.getMessage());
        }
    }

    /**
     * 读取 Gmail 邮箱
     */
    @GetMapping("/read/gmail")
    public ResponseEntity<String> readGmailInbox(@RequestParam String email,
                                                 @RequestParam String password) {
        try {
            gmailMailReader.readInbox(email, password);
            return ResponseEntity.ok("✅ Gmail 邮件读取成功");
        } catch (Exception e) {
            return ResponseEntity.status(500).body("❌ 邮件读取失败: " + e.getMessage());
        }
    }
}
